import 'package:flutter/material.dart';
import 'package:x3_charts_core/chart_painter/reverse_histogram_chart_painter.dart';
import 'package:x3_charts_core/model/general_chart_model.dart';

///**
///* @author hrongyong
///* @date   3/4/21
///* @email hrongyong@gmail.com
///* Description: 反向直方图
///*/
class ReverseHistogramChartWidget extends StatefulWidget {
  ///图表数组
  final List<GeneralChartModel> chartList;

  ///Y轴示例 - 用于测量Y轴系数宽度
  final String yAxisExample;

  ///背景颜色
  final Color backgroundColor;

  ///左边X轴最大值
  final int leftXAxisMax;

  ///右边X轴最大值
  final int rightXAxisMax;

  ///绘制X轴线
  final bool drawXAxis;

  ///绘制X轴线刻度
  final bool drawXAxisScale;

  ///绘制Y轴线
  final bool drawYAxis;

  ///绘制Y轴线刻度
  final bool drawYAxisScale;

  ///左边直方图颜色
  final Color leftHistogramColor;

  ///右边直方图颜色
  final Color rightHistogramColor;

  ///是否显示描述(直方图两边的描述)
  final bool showDescription;

  ///显示几个横条 - 默认不传 - 不传即为按数组长度取值
  ///当时数组长度不够时居中处理
  final int showSeveralBars;

  ReverseHistogramChartWidget({
    @required this.chartList,
    @required this.leftXAxisMax,
    @required this.rightXAxisMax,
    @required this.yAxisExample,
    this.backgroundColor = Colors.white,
    this.drawXAxis = false,
    this.drawXAxisScale = true,
    this.drawYAxis = false,
    this.drawYAxisScale = true,
    this.leftHistogramColor = const Color(0xFFDAABBC),
    this.rightHistogramColor = const Color(0xFFA2C1D1),
    this.showDescription = true,
    this.showSeveralBars,
  });

  @override
  _ReverseHistogramChartWidgetState createState() => _ReverseHistogramChartWidgetState();
}

class _ReverseHistogramChartWidgetState extends State<ReverseHistogramChartWidget> with SingleTickerProviderStateMixin {
  AnimationController _controller;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: const Duration(milliseconds: 1000),
      vsync: this,
    )..forward();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (BuildContext context, BoxConstraints constraints) {
        return CustomPaint(
          size: Size(constraints.maxWidth, constraints.maxHeight),
          painter: ReverseHistogramChartPainter(
              chartList: widget.chartList,
              repaint: _controller,
              yAxisExample: widget.yAxisExample,
              leftXAxisMax: widget.leftXAxisMax,
              rightXAxisMax: widget.rightXAxisMax,
              backgroundColor: widget.backgroundColor,
              drawXAxis: widget.drawXAxis,
              drawXAxisScale: widget.drawXAxisScale,
              drawYAxis: widget.drawYAxis,
              drawYAxisScale: widget.drawYAxisScale,
              leftHistogramColor: widget.leftHistogramColor,
              rightHistogramColor: widget.rightHistogramColor,
              showDescription: widget.showDescription,
              showSeveralBars: widget.showSeveralBars),
        );
      },
    );
  }
}
